//设置cookie
function SetCookie(name, keyword) {
    document.cookie = name + "=" + keyword;
}

//获取cookie
function GetCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
}

// 打开主弹出层
function OpenPeople() {
    //打开主弹出层
    layui.use('layer', function() {
        var layer = layui.layer;
        layer.tab({
            type: 1,
            resize: true,
            scrollbar: false,
            area: ['600px', '500px'],
            offset: ['60px'],
            tab: [{
                title: '公告',
                content: "网站内容正在陆续更新中"
            }, {
                title: '本期封面人物',
                content: "<div class='layui-collapse'><div class='layui-colla-item'><h2 class='layui-colla-title'>开源指北</h2><div class='layui-colla-content layui-show'>开源精神到底是什么</div></div><button type='button' class='layui-btn layui-btn-radius layui-btn-normal' onclick='ToPeople()'>去查看</button></div><script>layui.use('element', function() {var element = layui.element;});</script>"
            }, {
                title: '最近更新文件',
                content: "<button type='button' class='layui-btn layui-btn-radius layui-btn-normal' onclick='OpenMyFileNow()'>查看</button>"
            }]
        });
    });
}

//打开网易云音乐
function OpenMusic() {
    //打开网易云音乐弹出层
    layui.use('layer', function() {
        var layer = layui.layer;
        layer.open({
            title: '音乐',
            type: 2,
            shade: 0,
            offset: 'lb',
            anim: 5,
            area: ['338px', '542px'],
            resize: true,
            content: 'http://music.163.com/outchain/player?type=0&amp;id=19723756&amp;auto=0&amp;height=430'
        });
    });

}

//去封面人物
function ToPeople() {
    window.location.href = "http://localhost:3000/#/people/home"
}



//打开评论展示按钮
function OpenComment() {
    layui.use('util', function() {
        var util = layui.util;

        //执行
        util.fixbar({
            bar1: true,
            click: function(type) {
                console.log(type);
                if (type === 'bar1') {
                    OpenCommentUi()
                }
            }
        });
    });
}


//打开评论
function OpenCommentUi() {
    layui.use('layer', function() {
        var layer = layui.layer;
        layer.open({
            title: '留言',
            type: 2,
            shade: 0,
            offset: 'rb',
            anim: 5,
            area: ['338px', '542px'],
            resize: true,
            content: 'http://127.0.0.1:5500/html/comment.html'
        });
    });
}

//获取参数的值
function getUrlParam(name) {
    //构造一个含有目标参数的正则表达式对象
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    //匹配目标参数
    var r = window.location.search.substr(1).match(reg);
    //返回参数值
    if (r != null) {
        return decodeURI(r[2]);
    }
    alert("pdf文件不存在")
    window.history.back(-1);
}

//打开最近更新文件

function OpenMyFileNow() {
    var url = window.location.protocol + "//" + window.location.host + "#/pdf?name=java学习手册"
    window.location.href = url
}


//设置密码访问
function OpenPass(pass) {
    url = window.location.href
    if (GetCookie('passurl') != url) {
        window.history.back(-1);
        var index = layer.load(2); //换了种风格
        layer.prompt({
            formType: 1,
            value: '',
            title: '该页面需要您输入密码访问(测试功能)',
            area: ['800px', '350px'],
            cancel: function(index, layero) {
                layer.close(index)
            }
        }, function(value, index, elem) {
            if (value == pass) {
                window.location.href = url
                layer.close(index);
                SetCookie('passurl', url)
            } else {
                layer.msg("密码错误")
                layer.close(index)
            }

        });
        layer.close(index)
    }
}

//获取时间
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
        " " + date.getHours() + seperator2 + date.getMinutes() +
        seperator2 + date.getSeconds();
    return currentdate;
}

//获取ip
function GetMyIp() {
    url = 'https://pv.sohu.com/cityjson';
    axios.get(url)
        .then(res => {
            console.log(res.date)
        })
        .error(err => {
            alert('error')
        })
        // console.log(axios)
}

//设置游客模式
function OpenVisitor() {
    var url = document.URL;
    if (!GetCookie('name')) {
        window.history.back(-1);
        var index = layer.load(2); //换了种风格
        layer.prompt({
            formType: 0,
            value: '',
            title: '该页面开启了游客模式，请输入您的姓名(测试功能)',
            area: ['800px', '350px'],
            cancel: function(index, layero) {
                layer.close(index)
            }
        }, function(value, index, elem) {

            window.location.href = url
            layer.close(index);
            SetCookie('name', value)
            var time = getNowFormatDate()
            url = window.location.href;
            var content = '姓名:' + value + '           ' + '内容:' + value + '在' + time + '访问你的网站。</br>' + '访问链接为:' + url
            axios
                .post('https://api.dzzui.com/api/mail?Host=smtp.qq.com&Username=polarislovetop@qq.com&Password=pabqwbqicljmicfb&Port=465&SMTPSecure=ssl&addAddress=2283761246@qq.com&title=' + value + '访问您的网站' + '&text=' + content)
                .then(res => {})
                .catch(function(error) { // 请求失败处理
                    console.log(error);
                });
        });
        layer.close(index)
    }
}